//==============================================================================
// 21AT-WebGIS平台客户端程序，版权所有，二十一世纪空间技术应用股份有限公司，2000-2009。
// 作者： 
// 修改：	 
// 文件名：DrawRectFeature.js  
// 功能：绘制长方形框
// 最后修改时间：
//==============================================================================
/**
 * @requires OpenLayers/Control/DrawFeature.js
 */
OpenLayers.Control.DrawRectFeature = OpenLayers.Class(OpenLayers.Control.DrawFeature, {
	handlerclass: null,
	type : OpenLayers.Control.TYPE_TOOL,
	title : '框选',
	/**
     * Constructor: OpenLayers.Control.DrawFeature
     * 
     * Parameters:
     * layer - {<OpenLayers.Layer.Vector>} 
     * options - {Object} 
     */
    initialize: function(options) {
    	this.layer = options.layer;
    	
    	var handler = OpenLayers.Handler.RectPolygon;
    	this.handlerclass = handler;
        
        OpenLayers.Control.prototype.initialize.apply(this, [options]);
        
        this.callbacks = OpenLayers.Util.extend(
            {
                done: this.drawFeature,
                modify: function(vertex, feature) {
                    this.layer.events.triggerEvent(
                        "sketchmodified", {vertex: vertex, feature: feature}
                    );
                },
                create: function(vertex, feature) {
                    this.layer.events.triggerEvent(
                        "sketchstarted", {vertex: vertex, feature: feature}
                    );
                }
            },
            this.callbacks
        );
        
        this.handlerOptions = this.handlerOptions || {};
        if (!("multi" in this.handlerOptions)) {
            this.handlerOptions.multi = this.multi;
        }
        var sketchStyle = this.layer.styleMap && this.layer.styleMap.styles.temporary;
        if(sketchStyle) {
            this.handlerOptions.layerOptions = OpenLayers.Util.applyDefaults(
                this.handlerOptions.layerOptions,
                {styleMap: new OpenLayers.StyleMap({"default": sketchStyle})}
            );
        }
        
        this.handler = new this.handlerclass(this, this.callbacks, this.handlerOptions);
    },
    
    /**
     * APIMethod: activate
     * Activate the control and the feature handler.
     * 
     * Returns:
     * {Boolean} Successfully activated the control and feature handler.
     */
    activate: function() {
        if(!this.layer){
        	this.layer = this.map.defaultVLayer;
        }
        this.map.layerContainerDiv.className = "DrawRectCur";
        this.map.layerContainerDiv.style.cursor = "url("
				+ OpenLayers.Util.getRootPath()
				+ "img/rect.cur),default";
        return OpenLayers.Control.prototype.activate.apply(this,arguments);
    },
    
    /**
     * Method: drawFeature
     */
    drawFeature: function(geometry) {
        var feature = new OpenLayers.Feature.Vector(geometry);
        var proceed = this.layer.events.triggerEvent(
            "sketchcomplete", {feature: feature}
        );
        if(proceed !== false) {
            feature.state = OpenLayers.State.INSERT;
            this.layer.addFeatures([feature]);
            this.featureAdded(feature);
            this.events.triggerEvent("featureadded",{feature : feature});
        }
    },
    
    deactivate: function(){
    	this.map.layerContainerDiv.style.cursor = this.map.defaultCursor;
    	OpenLayers.Control.DrawFeature.prototype.deactivate.apply(this,arguments);
    },
    
    CLASS_NAME: "OpenLayers.Control.DrawRectFeature"
});